package com.zlict.admin.service.impl;

import com.zlict.admin.domain.ProjectDevice;
import com.zlict.admin.mapper.ProjectDeviceMapper;
import com.zlict.admin.service.IProjectDeviceService;
import com.zlict.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.zlict.common.utils.SecurityUtils.getUserId;

/**
 * 项目设备管理Service业务层处理
 * 
 * @author dzw
 * @date 2025-06-16
 */
@Service
public class ProjectDeviceServiceImpl implements IProjectDeviceService 
{
    @Autowired
    private ProjectDeviceMapper projectDeviceMapper;

    /**
     * 查询项目设备管理
     * 
     * @param id 项目设备管理主键
     * @return 项目设备管理
     */
    @Override
    public ProjectDevice selectProjectDeviceById(Long id)
    {
        return projectDeviceMapper.selectProjectDeviceById(id);
    }

    /**
     * 查询项目设备管理列表
     * 
     * @param projectDevice 项目设备管理
     * @return 项目设备管理
     */
    @Override
    public List<ProjectDevice> selectProjectDeviceList(ProjectDevice projectDevice)
    {
        return projectDeviceMapper.selectProjectDeviceList(projectDevice);
    }

    /**
     * 新增项目设备管理
     * 
     * @param projectDevice 项目设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertProjectDevice(ProjectDevice projectDevice)
    {
        if (ObjectUtils.isEmpty(projectDevice)){
            throw new RuntimeException("参数错误");
        }
        if (projectDevice.getProjectId() == null){
            throw new RuntimeException("未获取到项目,添加失败");
        }
        projectDevice.setCreateTime(DateUtils.getNowDate());
        projectDevice.setCreateBy(getUserId());
        if (projectDevice.getDeviceIds() == null || projectDevice.getDeviceIds().size() == 0){
//            projectDevice.setCreateTime(DateUtils.getNowDate());
//            projectDevice.setCreateBy(getUserId());
//            return projectDeviceMapper.insertProjectDevice(projectDevice);
            throw new RuntimeException("未选择设备,不可添加");
        }else{
            int num = projectDevice.getDeviceIds().size();
            int sum = 0;
            for (int i = 0; i < num; i++) {
                projectDevice.setDeviceId(projectDevice.getDeviceIds().get(i));
                sum += projectDeviceMapper.insertProjectDevice(projectDevice);
            }
            return sum;
        }
    }

    /**
     * 修改项目设备管理
     * 
     * @param projectDevice 项目设备管理
     * @return 结果
     */
    @Override
    public int updateProjectDevice(ProjectDevice projectDevice)
    {

        projectDevice.setUpdateTime(DateUtils.getNowDate());
        projectDevice.setUpdateBy(getUserId());
        return projectDeviceMapper.updateProjectDevice(projectDevice);
    }

    /**
     * 批量删除项目设备管理
     * 
     * @param ids 需要删除的项目设备管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectDeviceByIds(Long[] ids)
    {
        return projectDeviceMapper.deleteProjectDeviceByIds(ids);
    }

    /**
     * 删除项目设备管理信息
     * 
     * @param id 项目设备管理主键
     * @return 结果
     */
    @Override
    public int deleteProjectDeviceById(Long id)
    {
        return projectDeviceMapper.deleteProjectDeviceById(id);
    }
}
